/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.motoo.codec;

import com.google.code.motoo.core.IoBuffer;
import com.google.code.motoo.core.IoSession;
import com.google.code.motoo.filter.IoFilter;
import com.google.code.motoo.utils.IoSessionUtils;

/**
 * 实现数据的编解码接口, 将编解码对象放入当前线程变量中，这样可减少重复创建多余的编解码对象，
 * 同时也可避免并发访问对象产生的错误
 * @author linfengqi
 * @version 1.0  May 11, 2009
 */
public class CodecFilter implements IoFilter{
	private ProtocolCodecFactory factory;
	private ProtocolEncoderOutput encoderOut;
	private ProtocolDecoderOutput decoderOut;
	private final ThreadLocal<ProtocolDecoder> decoderLocal = new ThreadLocal<ProtocolDecoder>();
	private final ThreadLocal<ProtocolEncoder> encoderLocal = new ThreadLocal<ProtocolEncoder>();
	
	public CodecFilter(ProtocolCodecFactory factory) {
		this.factory = factory;
		encoderOut = new DefaultProtocolEncoderOutput(true);
		decoderOut = new DefaultProtocolDecoderOutput();
	}

	public void messageReceived(NextIoFilter nextFilter, IoSession session, Object message) throws Exception {
		if (!(message instanceof IoBuffer)) {
			nextFilter.messageReceived(session, message);
			return;
		}
		IoBuffer in = (IoBuffer) message;
		
		ProtocolDecoder decoder = getDecoder();
		decoder.decode(session, in, decoderOut);
		decoderOut.flash(nextFilter, session);
	}

	public void messageSend(NextIoFilter nextFilter, IoSession session, Object message) throws Exception {
		IoSessionUtils.setThreadLocalIoSession(session);
		ProtocolEncoder encoder = getEncoder();
		encoder.encode(session, message, encoderOut);
		encoderOut.flash(nextFilter, session);
	}
	
	private ProtocolDecoder getDecoder() {
		ProtocolDecoder decoder = decoderLocal.get();
		if (decoder != null) return decoder;
		decoder = factory.getDecoder();
		decoderLocal.set(decoder);
		return decoder;
	}
	
	private ProtocolEncoder getEncoder() {
		ProtocolEncoder encoder = encoderLocal.get();
		if (encoder != null) return encoder;
		encoder = factory.getEncoder();
		encoderLocal.set(encoder);
		return encoder;
	}

	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		
	}

	public void init() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	
}
